package com.ultrawise.survey.module.surevybase.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.ultrawise.survey.common.utils.DateUtils;
import com.ultrawise.survey.core.Page;
import com.ultrawise.survey.core.PaginationHelper;
import com.ultrawise.survey.core.base.BaseDao;
import com.ultrawise.survey.core.base.Saveable;
import com.ultrawise.survey.module.surevybase.controller.TSurveyInfoBaseControl;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoBase;

/**
 * A data access object (DAO) providing persistence and search support for
 * TSurveyInfoBase entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see TSurveyInfoBaseControl.dao.TSurveyInfoBase
 * @author MyEclipse Persistence Tools
 */

@Repository("tSurveyInfoBaseDAO")
public class TSurveyInfoBaseDAO extends BaseDao {
	private static final Logger log = LoggerFactory.getLogger(TSurveyInfoBaseDAO.class);

	// 根据条件查询案件表中的数据
	public Page<TSurveyInfoBase> getlistpage(int start, int limel,TSurveyInfoBase baseinfo)
	{
		PaginationHelper<TSurveyInfoBase> ph = new PaginationHelper<TSurveyInfoBase>();
		String sql = " from t_survey_info_base a where 1 = 1 ";
		Object[] params = new Object[7];
		int index = 0;
		//添加基础的调查名称
		if (!StringUtils.isEmpty(baseinfo.getSy_name())) {
			sql += " and a.SY_NAME like ? ";
			params[index] = "%" + baseinfo.getSy_name() + "%";
			index++;
		}
		Object[] paramsEnd = new Object[index];
		System.arraycopy(params, 0, paramsEnd, 0, index);
		Page<TSurveyInfoBase> pg= ph.fetchPage(getJdbcTemplate(), "select count(*) " + sql, "select * " + sql, paramsEnd, start,
				limel, new BeanPropertyRowMapper<TSurveyInfoBase>(TSurveyInfoBase.class));
		return pg;
	}
	
	public List<TSurveyInfoBase> getStarSurveyInfo(){
		String nowDate =DateUtils.getDate(null);
		String sql = " from t_survey_info_base where str_to_date('"+nowDate+"','%Y-%m-%d')>=str_to_date(SY_STARTIME,'%Y-%m-%d %H:%i:%s') and str_to_date('"+nowDate+"','%Y-%m-%d')<=str_to_date(SY_ENDTIME,'%Y-%m-%d %H:%i:%s') and SY_ISPUB=1 and (SY_STATUS=0 or SY_STATUS=1)";
		
		PaginationHelper<TSurveyInfoBase> ph = new PaginationHelper<TSurveyInfoBase>();
		
		Page<TSurveyInfoBase> pg= ph.fetchPage(getJdbcTemplate(), "select count(*) " + sql, "select * " + sql, null, 1,
				10000, new BeanPropertyRowMapper<TSurveyInfoBase>(TSurveyInfoBase.class));
		 return pg.getPageItems();
	}
}